package com.itfreer.data;

import com.itfreer.data.metadata.MetadataSynService;
import com.itfreer.data.organize.OrganizeSynService;

/**
 * 定义配置同步服务类
 */
public class ConfigSynService {
    public static final ConfigSynService instance = new ConfigSynService();
    private ConfigSynService() {
    }

    /**
     * 配置同步
     * @param listener
     */
    public void synConfig(final ConfigSynProgressListener listener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(listener!=null){
                    try {
                        listener.onSize(6,1, "开始下载政区配置！");
                        if(OrganizeSynService.instance.synAdministrative()){
                            listener.onSize(6,1, "下载政区配置成功！");
                        } else {
                            listener.onSize(6,1, "下载政区配置失败！");
                            Thread.sleep(1500);
                        }

                        listener.onSize(6,2, "开始下载机构配置！");
                        if(OrganizeSynService.instance.synOrganization()){
                            listener.onSize(6,2, "下载机构配置成功！");
                        }else{
                            listener.onSize(6,2, "下载机构配置失败！");
                            Thread.sleep(1500);
                        }

                        listener.onSize(6,3, "开始下载元数据目录配置！");
                        if(MetadataSynService.instance.synDirectory()){
                            listener.onSize(6,3, "下载元数据目录成功！");
                        }else {
                            listener.onSize(6,3, "下载元数据目录失败！");
                            Thread.sleep(1500);
                        }

                        listener.onSize(6,4, "开始下载元数据配置！");
                        if(MetadataSynService.instance.synMetadata()){
                            listener.onSize(6,4, "下载元数据成功！");
                        }else {
                            listener.onSize(6,4, "下载元数据失败！");
                            Thread.sleep(1500);
                        }

                        listener.onSize(6,5, "开始下载字典配置！");
                        if(MetadataSynService.instance.synDictionary()){
                            listener.onSize(6,5, "下载字典成功！");
                        }else{
                            listener.onSize(6,5, "下载字典失败！");
                            Thread.sleep(1500);
                        }

                        listener.onSize(6,6, "配置同步完成！");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    OrganizeSynService.instance.synOrganization();
                    OrganizeSynService.instance.synAdministrative();

                    MetadataSynService.instance.synDirectory();
                    MetadataSynService.instance.synMetadata();
                    MetadataSynService.instance.synDictionary();
                }
            }
        }).start();
    }
}
